import Vue from "vue";
export default {
	namespaced: true, // 命名空间必须写
	state: {
		isLoading: true,
		bannerList: [],
		productCategoryList: [],
		medicalCosmetologyZoneList: [],
		recommendProductList: [],
		brandCategoryList: [],
		brandList: [],
		seckillList: [],
		crossBorderList: [],
		activityProductList: [],
		integralRecommendList: [],
		couponList: [],
		activityByScene: null,
		medicalCosmetologyZoneProductList: [],
		medicalCosmetologyZoneLoading: true,
		bannerListMedical: [],
		userInfo: uni.getStorageSync('userInfo') || {}, // 用户信息
		navbarHeight: 0,
		productList: [],
		loadMoreStatus: 1,
		total: 0,
		productLoading: true,
		is_new_user: 0,
		page: 1,
		activityList: [] // 活动列表
	},
	getters: {
		getState(state) {
			return state;
		}
	},
	// 唯一修改state值的方法 
	// commit
	mutations: {
		setData(state, val) {
			state.bannerList = val.bannerList
			// #ifdef MP-WEIXIN 
			state.productCategoryList = val.categoryList
			// #endif
			// #ifdef APP-PLUS || H5
			state.productCategoryList = val.categoryList.filter(item => item.name != '跨境专区');
			// #endif
			state.brandList = val.brandList
			state.medicalCosmetologyZoneList = val.medicalCosmetologyZoneList
			state.integralRecommendList = val.integralProduct;
			state.recommendProductList = val.recommendProduct;
			state.seckillList = val.seckillList
			state.crossBorderList = val.crossBorderList
			state.activityProductList = val.activityProductList
			state.brandCategoryList = val.brandCategoryList
			state.couponList = val.couponList
			state.bannerListMedical = val.bannerListMedical
			state.is_new_user = val.is_new_user
		},
		setIsLoading(state, val) {
			state.isLoading = val;
		},
		setActivityByScene(state, val) {
			state.activityByScene = val;
		},
		// setRecommendProduct(state, val) {
		// 	state.recommendProductList = val;
		// },
		setMedicalCosmetologyZoneProductList(state, val) {
			state.medicalCosmetologyZoneProductList = val
		},
		setMedicalCosmetologyZoneLoading(state, val) {
			state.medicalCosmetologyZoneLoading = val
		},
		setUserInfo(state, value) {
			state.userInfo = value;
			Vue.prototype.$setCache('userInfo', state.userInfo)
		},
		setNavbarHeight(state, value) {
			state.navbarHeight = value
		},
		setProductList(state, value) {
			state.productList.push(...value);
		},
		setTotal(state, value) {
			state.total = value
		},
		setLoadMoreStatus(state, value) {
			state.loadMoreStatus = Number(value)
		},
		setPage(state, value) {
			state.page = value
		},
		setActivity(state, value) {
			state.activityList = value
		}
	},
	// 异步的操作
	actions: {
		// 获取主体信息
		getIndex(context) {
			Vue.prototype.$get("index/indexData").then(res => {
				context.commit('setData', res.data)
				context.commit('setUserInfo', res.data.user_info)
				context.dispatch('getActivityByScene')
				if (Vue.prototype.$isLogin() && context.rootState.user.user_type == 1) { // 用户登录
					// 获取用户信息
					context.dispatch('user/getUserInfo', 1, {
						root: true
					});
				}
				if (Vue.prototype.$isLogin() && context.state.couponList.length > 0) {
					setTimeout(() => {
						context.commit('messageBox/setVisible', true, {
							root: true
						});
					}, 2000);
				}
			}).catch(ex => {
				Vue.prototype.$errorToast(ex);
			})
		},
		getActivityByScene(context) {
			Vue.prototype.$get("redPacket/getActivityByScene", {
				receive_scene: 1
			}).then(res => {
				context.commit('setActivityByScene', res.data)
				context.dispatch('getMedicalCosmetologyZone')
				// 获取个性化推荐商品
				context.dispatch('guessRecommend/getRecommendList', {}, {
					root: true
				});
				context.commit('setIsLoading', false)
			}).catch(ex => {
				Vue.prototype.$errorToast(ex);
			})
		},
		// 获取专区商品列表
		getMedicalCosmetologyZone(context, params) {
			// 检查 medicalCosmetologyZoneList 是否为空
			if (!context.state.medicalCosmetologyZoneList || context.state.medicalCosmetologyZoneList.length === 0) {
				console.error('医美专区列表为空');
				return Promise.reject(new Error('医美专区列表为空'));
			}
			// 设置加载状态为 true
			context.commit('setMedicalCosmetologyZoneLoading', true);
			let id;
			if (params !== undefined && params < context.state.medicalCosmetologyZoneList.length) {
				id = context.state.medicalCosmetologyZoneList[params].id;
			} else {
				id = context.state.medicalCosmetologyZoneList[0].id;
			}
			return Vue.prototype.$get("medicalCosmetologyZone/productList", {
					id: id,
					pageSize: 9
				})
				.then(res => {
					if (params === undefined) {
						context.dispatch('getActivityList')
					}
					context.commit('setMedicalCosmetologyZoneProductList', res.data.data);
					context.commit('setMedicalCosmetologyZoneLoading', false);
					return 'ok';
				})
				.catch(ex => {
					// 设置加载状态为 false
					context.commit('setMedicalCosmetologyZoneLoading', false);
					Vue.prototype.$errorToast(ex);
					return Promise.reject(ex);
				});
		},
		// 获取活动列表
		getActivityList(context) {
			Vue.prototype.$get("activity/list").then(res => {
				context.commit('setActivity', res.data)
				context.dispatch('getProductList', context.state.page)
			}).catch(ex => {
				Vue.prototype.$errorToast(ex);
			})
		},
		// 获取商品列表
		getProductList(context, page) {
			context.commit('setPage', page)
			Vue.prototype.$get("product/list", {
				page: context.state.page || 1
			}).then(res => {
				context.state.productLoading = false
				context.commit('setProductList', res.data.data)
				context.commit('setTotal', res.data.total)
				if (context.state.total == context.state.productList.length) {
					context.commit('setLoadMoreStatus', 3) // 分页结束
				} else {
					context.commit('setLoadMoreStatus', 1) // 上滑加载
				}
			}).catch(ex => {
				context.state.productLoading = false
				Vue.prototype.$errorToast(ex);
			})
		},
	}
}